package com.zhangfei.sys.controller;

import com.zhang.entity.*;
import com.zhangfei.cilent.IUserClient;
import com.zhangfei.sys.service.CultivateService;
import com.zhangfei.sys.service.EvaluationService;
import com.zhangfei.sys.service.ExamineService;
import com.zhangfei.sys.service.FeedbackService;
import com.zhangfei.util.ExcelUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: 96199
 * @className: OrderController
 * @description:
 * @date: 2021/6/23 10:08
 * @version: 0.1
 * @since: jdk1.8
 */
@RestController
@RequestMapping("/cul")
public class CultivateController {

    @Resource
    private IUserClient iUserClient;
    @Resource
    private CultivateService cultivateService;

    @Resource
    private ExamineService examineService;
    @Resource
    private EvaluationService evaluationService;

    @Resource
    private FeedbackService feedbackService;

    /**
     *
     * @param tid
     * @param file 培训计划读取
     * @return
     */
    @PostMapping("/{tid}")
    public Retuntity   queryUser(@PathVariable("tid") String tid, MultipartFile file){
        List<Cultivate> cultivates = new ArrayList<>();
        try {
            List<Map<String, Object>> maps = ExcelUtil.readXlsx(file.getInputStream(), 0, Cultivate.class);
            System.out.println(maps);
            assert maps != null;
            for (Map<String, Object> map : maps) {
                Employee employee = new Employee();
                employee.setPhone(map.get("电话").toString());
                employee.setName(map.get("姓名").toString());
//                System.out.println(employee);
                Employee emPl = iUserClient.getEmPl(employee);
                Cultivate cultivate = new Cultivate();
                cultivate.setCreateTime(new Date());
                cultivate.setEid(emPl.getEid().intValue());
                cultivate.setCreateId(Integer.valueOf(tid));
                cultivate.setDepartId(Integer.valueOf(emPl.getDepartId()));
                cultivate.setCultivate(map.get("培养计划").toString());
                cultivates.add(cultivate);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(cultivates);
        cultivateService.insertBatch(cultivates);
        return Retuntity.success("添加培训计划成功");
    }

    @GetMapping("/{uid}")
    public Retuntity queryTeacher(@PathVariable("uid") String uid){
        Employee user = iUserClient.getEm(uid);
        System.out.println(user);
        Employee teacher = iUserClient.getEm(user.getTid().toString());
        Cultivate cultivate = cultivateService.queryByUserId(Integer.valueOf(uid));
        user.setTeacher(teacher);
        user.setCultivate(cultivate);
        return new Retuntity(user,200,"查询成功");
    }

    @PutMapping("/exam")
    public Retuntity insertExam(@RequestBody Examine examine){
        examineService.insert(examine);
        return Retuntity.success("评价添加成功");
    }

    @PutMapping("/addEva")
    public Retuntity insertEva(@RequestBody Evaluation evaluation){
        evaluationService.insert(evaluation);
        return Retuntity.success("评分成功");
    }

    @GetMapping("/queryEva/{uid}/{tid}")
    public Retuntity query(@PathVariable("uid") String uid, @PathVariable("tid") String tid){
        return evaluationService.queryAll(uid,tid);
    }

    @PostMapping("/insertFb")
    public Retuntity insertFb(@RequestBody Feedback feedback){
        feedbackService.insert(feedback);
        return Retuntity.success("新增反馈成功");
    }
}
